package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	redis "zc1/model/reids"
	"zc1/zcrpc/internal/svc"
	"zc1/zcrpc/zcrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ZcSendsmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewZcSendsmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ZcSendsmsLogic {
	return &ZcSendsmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ZcSendsmsLogic) ZcSendsms(in *zcrpc.ZcSendsmsRequest) (*zcrpc.ZcSendsmsResponse, error) {
	// todo: add your logic here and delete this line
	rediskeyone := redis.Client.Get("Smsrediskey" + in.Phone)
	if rediskeyone.Val() >= "1" {
		return nil, fmt.Errorf("一分钟内发一次")
	}
	code := rand.Intn(9000) + 1000
	/*	sms, err := pkg.SendSMs(in.Phone, strconv.Itoa(code))
		if err != nil {
			return nil, err
		}
		if *sms.Body.Code != "OK" {
			return nil, fmt.Errorf(*sms.Body.Message)
		}*/
	redis.Client.Set("Sms"+in.Source+in.Phone, code, time.Minute*5)
	rediskey := redis.Client.Incr("Smsrediskey" + in.Phone)
	if rediskey.Val() == 1 {
		redis.Client.Expire("Smsrediskey"+in.Phone, time.Minute*1)
	}
	return &zcrpc.ZcSendsmsResponse{}, nil
}
